import { Injectable } from "@angular/core";
import { UserDto } from "../../shared/models/dto/user.dto";
import { SqliteService } from "../../shared/services/sqliteDB/sqlite.service";
import { SecureStorageService } from '../../shared/services/secure-storage.services';
import { HttpService } from 'http-service';
import { ConfigService } from "../../shared/services/config.service";
import { UserService } from "../../shared/models/services/user.service";
import { SyncService } from "../../shared/services/offline/sync.servie";
import { StorageService } from "../../shared/models/services/storage.service";

import { CurdService } from "../../shared/models/services/curd.service";
import { Attachment } from "../../shared/models/transform/attachment";
import { AttachmentDto } from "../../shared/models/dto/attachment.dto";
import { Bill } from "../../shared/models/transform/bill";
import { BillDto } from "../../shared/models/dto/bill.dto";
import { Device } from "../../shared/models/transform/device";
import { DeviceDto } from "../../shared/models/dto/device.dto";
import { District } from "../../shared/models/transform/district";
import { SysUser } from "../../shared/models/transform/sysUser";
import { SysUserDto } from "../../shared/models/dto/sysUser.dto";
import { WorkOrder } from "../../shared/models/transform/work-order";
import { WorkOrderDto } from "../../shared/models/dto/work-orders.dto";
import { CustomStorage } from "../../shared/models/transform/custom-storage";
import { StorageDto } from "../../shared/models/dto/storage.dto";
import { User } from "../../shared/models/transform/user";
import { DistrictDto } from "../../shared/models/dto/district.dto";
import { AreaDto } from "../../shared/models/dto/area.dto";
import { API } from "../../shared/models/api-path-extension";
import { ElectricityMeterDto } from "../../shared/models/dto/electricity-meter.dto";
import { InspectionAttachmentsDto } from "../../shared/models/dto/inspection/inspection-attachments.dto";
import { InspectionManualsDto } from "../../shared/models/dto/inspection/inspection-manuals.dto";
import { InspectionTasksDto } from "../../shared/models/dto/inspection/inspection-tasks.dto";
import { InspectionRecordsDto } from "../../shared/models/dto/inspection/inspection-records.dto";
import { InspectionOperationsDto } from "../../shared/models/dto/inspection/inspection-operations.dto";
import { InspectionOperationsTicketsDto } from "../../shared/models/dto/inspection/inspection-operation-tickets.dto";
import { ObjectDto } from "../../shared/services/sqliteDB/object.dto";
import { WorkTicketDto } from "../../shared/models/dto/inspection/work-ticket.dto";
import { InspectionSysUserDto } from "../../shared/models/dto/inspection/inspection-sysUser.dto";
import { InspectionPersonnelDto } from "../../shared/models/dto/inspection/inspection-personnel.dto";
import { InspectionCompanyDto } from "../../shared/models/dto/inspection/inspection-company.dto";
import { InspectionTypeDto } from "../../shared/models/dto/inspection/inspection-type.dto";
import { UserFeatureDto } from "../../shared/models/dto/user-feature.dto";
import { UserCharacteristicRelationshipDto } from "../../shared/models/dto/user-characteristic-relationship.dto";
declare let window: any;

@Injectable()
export class MainService {

  constructor(
    public config: ConfigService,
    private httpService: HttpService,
    private secureStorageService: SecureStorageService,
    public sqliteService: SqliteService,
    public userService: UserService,
    public curdService: CurdService,
    public syncService: SyncService
  ) {
    // this.secureStorageService.remove("access_token");
  }

  simpleFetch() {
    this.curdService.simpleFetch(UserDto).then((results) => {
      console.log(results);
    });
    this.curdService.simpleFetch(BillDto).then((results) => {
      console.log(results);
    });
  }

  fetchByCondition() {
    this.curdService.fetchByCondition(BillDto,
      {
        and: {
          // id: 4
          isSettle: 0
        },
        or: [{
          month: 6,
          year: 2018
        }, {
          month: 5,
          year: 2018
        }]
      },
      ["billDate"]);
  }

  addUser() {
    // user表
    let data = { propertyNo: "123313", areaId: "账号", customerName: "loki" , test: false};
    this.curdService.addRow({
      dto: UserDto,
      mapObj: User
    }, data);
  }

  update() {
    this.curdService.update({
      dto: UserDto,
      mapObj: User
    },
      { propertyNo: "123313", areaId: "账号", customerName: "loki", test: false }, {
        and: {
          id: 4
        }
      });
  }

  remove() {
    // this.userService.remove();
  }

  addColumn() {
    this.sqliteService.addColumn(UserDto, "test boolean");
  }

  dropTable() {
    let allTables = [
      UserDto,
      SysUserDto,
      DistrictDto,
      WorkOrderDto,
      BillDto,
      AreaDto,
      DeviceDto,
      StorageDto,
      ElectricityMeterDto,
      AttachmentDto,
      UserFeatureDto,
      UserCharacteristicRelationshipDto,

      // inspection 计划检修
      InspectionAttachmentsDto,
       InspectionManualsDto,
       InspectionTasksDto,
       InspectionRecordsDto,
       InspectionOperationsDto,
       InspectionOperationsTicketsDto,
       WorkTicketDto,
       InspectionSysUserDto,
       InspectionPersonnelDto,
       InspectionCompanyDto,
       InspectionTypeDto
    ];

    allTables.forEach((table: { new(): ObjectDto }) => this.curdService.dropTable(table));
  }

  getAll() {

    let user = {
      name: "user_cQdR2E", // 审批人
      password: "cobz7j"
    };

    let formData = new FormData();
    formData.append("grant_type", "password");
    formData.append("loginAccount", user.name);
    formData.append("loginPassword", user.password);
    formData.append("loginType", '3');


    this.secureStorageService.get("access_token").subscribe((token) => {
      if (token) {
        this.syncService.fetchAppData();
      } else {
        this.httpService.post(new API().path({ path: "token" }).baseURL, formData, null, {})
          .subscribe((data) => {
            if (!window.plugin) {
              this.secureStorageService.set("access_token", data.access_token).subscribe(() => {
                this.syncService.fetchAppData();
              });
            }
          });
      }
    });
  }
}
